package de.breitbeil.samsyncro.lib.util;

import java.io.IOException;

import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;

public class ConsoleDiscovery implements DiscoveryListener, Runnable {

    private boolean completed = false;
    private int count = 0;
    
    @Override
    public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
        System.out.println("Device "+count);
        System.out.println("---------");
        System.out.println("  Class:  "+cod.getMajorDeviceClass());
        System.out.println("  Adress: "+btDevice.getBluetoothAddress());
        try {
            System.out.println("  Name:   "+btDevice.getFriendlyName(false));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println();
        
        count = count++;
    }

    @Override
    public void inquiryCompleted(int discType) {
        completed = true;
    }

    @Override
    public void serviceSearchCompleted(int transID, int respCode) {
        // TODO Auto-generated method stub
    }

    @Override
    public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public void run() {
        try{
            LocalDevice local = LocalDevice.getLocalDevice();
            System.out.println("Local bluetooth configuration");
            System.out.println("-----------------------------");
            System.out.println(" Name:    "+local.getFriendlyName());
            System.out.println(" Address: "+local.getBluetoothAddress());
            System.out.println();
            
            DiscoveryAgent discoveryagent = local.getDiscoveryAgent();
            discoveryagent.startInquiry(DiscoveryAgent.GIAC, this);
            
        }catch(Exception e){
            System.out.println(e);
            e.printStackTrace();
        }
    }

    public boolean isCompleted() {
        return completed;
    }

}
